package com.team20.map_demo.controller;

import com.team20.map_demo.config.FileUploadConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUploadController {
    @Autowired
    FileUploadConfig fileUploadConfig;
    @PostMapping("/file/upload")
    public String submitUpUpload(MultipartFile file, String fileDesc, ModelMap modelMap){
        String dir = fileUploadConfig.getDir();
        String[] types = fileUploadConfig.getTypes();
        String sss= "xXxxxx.pptx";
        String originalFilename = file.getOriginalFilename();

        String  fileName4Svae = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
        try {
            file.transferTo(new File(dir,fileName4Svae));
        } catch (IOException e) {
            e.printStackTrace();
        }

        modelMap.put("fileName4Svae",fileName4Svae);
        modelMap.put("originalFilename",originalFilename);

        return "success";
    }
}
